package 算法.leetcode.offer;

/**
 * @author lchenglong
 * @date 2022/3/21
 */
public class Offer47 {

    public static void main(String[] args) {
        int[][] arr = new int[][]{
                {1,3,1},
                {1,5,1},
                {4,2,1}
//                {1}
            };
        Offer47 instance = new Offer47();
        int ans = instance.maxValue(arr);
        System.out.println(ans);
    }
    public int maxValue(int[][] grid) {
        if (grid == null||grid.length==0)return 0;
        int m = grid.length,n=grid[0].length;
        for (int i=0;i<m;i++){
            for (int j=0;j<n;j++){
                if (i == 0 && j == 0)continue;
                else if (i == 0){
                    grid[i][j] += grid[i][j-1];
                } else if (j == 0) {
                    grid[i][j] += grid[i-1][j];
                } else {
                    grid[i][j] += Math.max(grid[i-1][j],grid[i][j-1]);
                }
            }
        }
        return grid[m-1][n-1];
    }




}
